package Jump;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class JumpPanel extends JPanel implements ActionListener, KeyListener {
    // 游戏常量
    public static final int WIDTH = 800;
    public static final int HEIGHT = 300;
    public static final int GROUND_HEIGHT = 80;
    public static final int INITIAL_OBSTACLE_FREQ = 100;//障碍物生成速度
    // 游戏状态
    public boolean isPlaying = false;
    public boolean isGameOver = false;
    public int score = 0;
    public int highScore = 0;
    // 游戏元素
    public Character character;
    public ArrayList<Obstacle> obstacles = new ArrayList<>();
    public ArrayList<Cloud> clouds = new ArrayList<>();
    // 游戏计时器
    public Timer gameTimer;
    public int obstacleTimer = 0;
    public int obstacleSpeed = 7;

    public JumpPanel() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(new Color(173, 216, 230));
        setFocusable(true);
        addKeyListener(this);
        // 初始化角色
        character = new Character(GROUND_HEIGHT);
        // 初始化云朵
        for (int i = 0; i < 5; i++) {
            clouds.add(new Cloud());
        }
        // 初始化游戏计时器
        gameTimer = new Timer(20, this);
        gameTimer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 绘制天空
        g.setColor(getBackground());
        g.fillRect(0, 0, WIDTH, HEIGHT);
        // 绘制云朵
        for (Cloud cloud : clouds) {
            cloud.draw(g);
        }
        // 绘制地面
        g.setColor(new Color(235, 206, 10, 255));
        g.fillRect(0, HEIGHT - GROUND_HEIGHT, WIDTH, GROUND_HEIGHT);
        // 绘制地面线条
        g.setColor(new Color(34, 139, 34));
        for (int i = 0; i < WIDTH; i += 30) {
            g.drawLine(i, HEIGHT - GROUND_HEIGHT, i + 15, HEIGHT - GROUND_HEIGHT);
        }
        // 绘制角色
        character.draw(g);
        // 绘制障碍物
        for (Obstacle obstacle : obstacles) {
            obstacle.draw(g);
        }
        // 绘制分数
        g.setColor(new Color(25, 25, 112));
        g.setFont(new Font("宋体", Font.BOLD, 20));
        g.drawString("得分: " + score, WIDTH - 150, 30);
        g.drawString("最高分: " + highScore, WIDTH - 150, 60);
        // 游戏开始提示
        if (!isPlaying && !isGameOver) {
            g.setColor(new Color(70, 130, 180));
            g.setFont(new Font("宋体", Font.BOLD, 30));
            g.drawString("跑酷游戏", WIDTH / 2 - 100, HEIGHT / 2 - 40);
            g.drawString("按空格键或向上键开始", WIDTH / 2 - 150, HEIGHT / 2);
            g.setFont(new Font("宋体", Font.PLAIN, 20));
            g.drawString("空格键/向上键: 跳跃", WIDTH / 2 - 80, HEIGHT / 2 + 40);
            g.drawString("向下键: 滑铲", WIDTH / 2 - 50, HEIGHT / 2 + 70); // 修改为滑铲
        }
        // 游戏结束界面
        if (isGameOver) {
            g.setColor(new Color(0, 0, 0, 150));
            g.fillRect(0, 0, WIDTH, HEIGHT);
            g.setColor(Color.WHITE);
            g.setFont(new Font("宋体", Font.BOLD, 40));
            g.drawString("游戏结束", WIDTH / 2 - 100, HEIGHT / 2 - 30);
            g.setFont(new Font("宋体", Font.PLAIN, 24));
            g.drawString("得分: " + score, WIDTH / 2 - 50, HEIGHT / 2 + 20);
            g.drawString("按空格键或向上键重新开始", WIDTH / 2 - 160, HEIGHT / 2 + 60);
        }
    }
    //主循环体
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isPlaying) {
            // 更新分数
            score++;
            // 更新角色位置
            character.update();
            // 更新云朵
            for (Cloud cloud : clouds) {
                cloud.update();
            }
            // 生成障碍物
            obstacleTimer++;
            int obstacleFreq = Math.max(INITIAL_OBSTACLE_FREQ - score / 100, 40);
            if (obstacleTimer > obstacleFreq) {
                obstacles.add(new Obstacle(obstacleSpeed));
                obstacleTimer = 0;
            }
            // 更新障碍物
            for (int i = 0; i < obstacles.size(); i++) {
                Obstacle obstacle = obstacles.get(i);
                obstacle.update(score);
                // 检测碰撞
                if (character.getBounds().intersects(obstacle.getBounds())) {
                    gameOver();
                }
                // 移除屏幕外的障碍物
                if (obstacle.x + obstacle.width < 0) {
                    obstacles.remove(i);
                    i--;
                }
            }
        }
        repaint();
    }

    private void gameOver() {
        isGameOver = true;
        isPlaying = false;
        if (score > highScore) {
            highScore = score;
        }
    }

    private void startGame() {
        isPlaying = true;
        isGameOver = false;
        score = 0;
        obstacles.clear();
        obstacleTimer = 0;
        character.resetPosition();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        // 空格键或向上键跳跃
        if (keyCode == KeyEvent.VK_SPACE || keyCode == KeyEvent.VK_UP) {
            if (!isPlaying && !isGameOver) {
                startGame();
            } else if (isGameOver) {
                startGame();
            } else {
                character.jump();
            }
        }
        // 向下键滑铲
        if (keyCode == KeyEvent.VK_DOWN) {
            character.slide();
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {}
    @Override
    public void keyTyped(KeyEvent e) {}
}